<?php

namespace Modules\Order\Entities;

use Modules\User\Entities\User;
use Modules\Order\Entities\Payment;
use Modules\Order\Entities\Delivery;
use Modules\Order\Entities\OrderGoods;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class Order extends Model
{
    use SoftDeletes;

    // 订单状态: 0临时保存订单; 1新订单; 2已支付; 3已接单; 4已发货; 5已收货; 6交易完成; 7取消订单; 8无效订单; 9已分单; 
    const STATUS_TEMP = 0; // 临时订单
    const STATUS_NEW = 1; // 新订单，待付款
    const STATUS_PAIED = 2; // 已支付订单，待接单
    const STATUS_ACCEPT = 3; // 已接单，待发货
    const STATUS_DELIVERED = 4; // 已发货，待收货
    const STATUS_RECEIVED = 5; // 已收货，待评价
    const STATUS_SUCCESS = 6; // 订单完成
    const STATUS_CANCEL = 7; // 已取消订单
    const STATUS_INVALID = 8; // 已作废订单
    const STATUS_SEPARATED = 9; // 已分单

    protected $fillable = ['sn', 'user_id', 'merchant_id', 'channel', 'postscript'];

    protected $casts = [
        'user_id' => 'integer',
        'merchant_id' => 'integer',
    ];

    protected $dates = ['accept_at', 'complete_at'];

    public function user()
    {
        return $this->hasOne(User::class);
    }

    public function goods()
    {
        return $this->hasMany(OrderGoods::class);
    }

    public function payment()
    {
        return $this->hasOne(Payment::class);
    }

    public function delivery()
    {
        return $this->hasOne(Delivery::class);
    }
}
